﻿#include"stdafx.h"
#ifdef LK_WINDOWS_WINDOWSEXCEPTION_
namespace LK
{
	namespace Windows
	{
		PWindowsException::PWindowsException(HRESULT code) :ptr<Exception>(new Windows::WindowsException(code)) {}
		HRESULT PWindowsException::GetErrorCode()const { return Check()->errorCode; }
		ptr<String> WindowsException::GetDescriptionByCode(HRESULT code)
		{
			switch (code)
			{
#ifdef _WINDOWS
			case S_OK:return L"操作成功";
			case E_UNEXPECTED:return L"意外的失败";
			case E_NOTIMPL:return L"未实现";
			case E_OUTOFMEMORY:return L"未能分配所需的内存";
			case E_INVALIDARG:return L"一个或多个参数无效";
			case E_NOINTERFACE:return L"不支持此接口";
			case E_POINTER:return L"无效指针";
			case E_HANDLE:return L"无效句柄";
			case E_ABORT:return L"操作已中止";
			case E_FAIL:return L"未指定的失败";
			case E_ACCESSDENIED:return L"访问被拒绝";
#endif
			default:return L"未知错误:"+ptr<String>(code);
			}
		}
		PWindowsException::PWindowsException()throw() :PException(new Windows::WindowsException()) {}
		PWindowsException::PWindowsException(ptr<String> description)throw() :ptr<Exception>(new Windows::WindowsException(description)) {}
		PWindowsException::PWindowsException(ptr<String> description, ptr<Exception> innerException)throw() :ptr<Exception>(new Windows::WindowsException(description,innerException)) {}
#ifdef _CLR //托管支持
		PWindowsException::PWindowsException(System::Exception^ e) :ptr<Exception>(new WindowsException(e)) {}
		PWindowsException::operator System::Exception ^ ()const { if (*this==null)return nullptr;return *Check(); }
		WindowsException::WindowsException(System::Exception^ e)//从托管异常初始化
			:errorCode(System::Runtime::InteropServices::Marshal::GetHRForException(e)), Exception(e->Source + e->Message)
		{       }
		WindowsException::operator System::Exception ^ ()const{//到托管异常的转换
			return gcnew System::Exception(System::String::Format("(HRESULT:0x{1:X8}) {0}", description.ToGcString(), errorCode), Inner.CastTo<PWindowsException>());
		}
#endif
		WindowsException::WindowsException(HRESULT code):Exception(WindowsException::GetDescriptionByCode(code)),errorCode(code){}

	}
}
#endif
